$(document).ready(function() {

//    var today = new Date();
//    var dd = today.getDate();
//    var mm = today.getMonth() + 1; //January is 0!
//
//    var yyyy = today.getFullYear();
//    if (dd < 10) {
//        dd = '0' + dd;
//    }
//    if (mm < 10) {
//        mm = '0' + mm;
//    }
//    today = dd + '-' + mm + '-' + yyyy;
//    $('#payment_date').val(today);
    edit_payment();
    delete_payment();
    $("#reset").click(function() {
        $("#payment_id").val("0");
        $('#payment').val('');
        $('#payment_date').val(today);
        $('#payment_description').val('');
    });
    var act_submit = false;
    $("#save_payment").click(function() {
        if (act_submit === false) {
            act_submit = true;
            payment_date = '';
            if ($("#payment_date").val() != "Ngày đặt cọc") {
                payment_date = convert_date_to_string($('#span_payment_date').data('datetimepicker').getLocalDate());
            } else {
                act_submit = false;
                $("#alert-error-payment").html('Vui lòng nhâp ngày đặt cọc!').show();
                hideError("#alert-error-payment");
                return false;
            }
            $.post("ajax/updatePayment.php", {
                customer_order_id: $("#payment_customer_order_id").val(),
                payment_id: $("#payment_id").val(),
                payment: $("#payment").val(),
                type_payment: $("#type_payment").val(),
                description: $("#payment_description").val(),
                payment_date: payment_date
            }, function(res) {
                act_submit = false;
                if (res.type == 0) {
                    $("#alert-success-payment").html(res.msg).show();
                    hideError("#alert-success-payment");
                    $("#payment").val("");
                    $("#payment_description").val("");
                    $("#payment_id").val("0");
                    $("#box_content_customer_order_payment").html(res.html);

                    edit_payment();
                    delete_payment();
                } else {
                    $("#alert-error-payment").html(res.msg).show();
                    hideError("#alert-error-payment");
                }
            }, "json");


        } else {
            return false;
        }
    });


});

function delete_payment() {
    var act_del = false;
    $(".btn_delete_payment").unbind('click');
    $(".btn_delete_payment").click(function() {
        if (confirm("Are you sure you want to delete this payment?")) {
            if (act_del === false) {
                act_del = true;
                $.post("ajax/deletePayment.php", {
                    payment_id: $(this).attr('rel')
                }, function(res) {
                    act_del = false;
                    alert(res.msg);
                    if (res.type == 0) {
                        $("#box_content_customer_order_payment").html(res.html);
                        edit_payment();
                        delete_payment();
                    }

                }, "json");


            } else {
                return false;
            }
        } else {
            return false;
        }
    });
}
function edit_payment() {
    var act_edit = false;
    $(".btn_edit_payment").unbind('click');
    $(".btn_edit_payment").click(function() {
        if (act_edit === false) {
            act_edit = true;
            $.post("ajax/getPaymentById.php", {
                payment_id: $(this).attr('rel')
            }, function(res) {
                act_edit = false;
                $("#payment_id").val(res.id);
                $("#payment").val(res.payment);
                $("#type_payment").val(res.type_payment);
                $("#payment_description").html(res.description);
                $("#payment_date").val(res.payment_date);
                $("#content_zen_payment").scrollTop(0);
            }, "json");


        } else {
            return false;
        }

    });
}
function refresh_payment_list() {
    $.post("ajax/getPaymentList.php", {
        payment_id: $(this).attr('rel')
    }, function(res) {
        $("#tbody_payment_list").html(res.html);
        $("#payment_parent").html(res.payment_list);
        $("#parent_payment_id").val(0);
        $("#payment_id").val(0)
        edit_payment();
        delete_payment();
        change_select_parent_payment();
    }, "json");
}
function show_payment_list() {
    var show_payment_list = false;
    $(".parent_payment_list").unbind('change');
    $('.parent_payment_list').on('change', function(e) {
        if (show_payment_list === false) {
            show_payment_list = true;
            $.post("ajax/getSelectCategory.php", {
                payment_id: $(this).attr('rel')
            }, function(res) {
                if (res.type == 0) {

                }
                show_payment_list = false;
            }, "json");

        }
    });
}
function action_transfer_money(item, source_customer_order_id, destination_customer_order_id) {
    var row_payment = $(item).parent().parent();
    $.post("ajax/actionTransferMoney.php", {
        action:'add',
        source_customer_order_id: source_customer_order_id,
        destination_customer_order_id: destination_customer_order_id
    }, function(res) {
        if (res.type == 0) {
            row_payment.remove();
            $("#box_content_customer_order_payment").html(res.html);
            edit_payment();
            delete_payment();
        }
        alert(res.msg);
    }, "json");
}
function action_retransfer_money(item,payment_id, source_customer_order_id, destination_customer_order_id) {
    var row_payment = $(item).parent().parent();
    $.post("ajax/actionTransferMoney.php", {
        action:'remove',
        payment_id: payment_id,
        source_customer_order_id: source_customer_order_id,
        destination_customer_order_id: destination_customer_order_id
    }, function(res) {
        if (res.type == 0) {
            row_payment.remove();
            $("#box_content_customer_order_payment").html(res.html);
            edit_payment();
            delete_payment();
        }
        alert(res.msg);
    }, "json");
}


 